<?php 
class Booms_model extends CI_Model{
	// Display all product
	function Display($start,$num){
		$q= $this->db->query("
			SELECT  p_id,p_name,tp.pcat_code,pcat_name,brand_name,material,size,color,tp.status,tp.is_hot,tp.is_home,is_sale,sale_type,sale_value
			FROM    temp_product as tp
			JOIN    product_category as pc 
			ON      tp.pcat_code = pc.pcat_code
			JOIN    brand as b 
			ON      b.brand_alias = tp.brandcode
			LIMIT   $start,$num ");
		
		if($q->num_rows() > 0){
			foreach($q->result() as $row){
				$data[]=$row;
			}
			return $data;
		}
	}
	
	// Display all sale
	function SaleShow($start,$num){
		$q= $this->db->query("
			SELECT  p_id,p_name,tp.pcat_code,pcat_name,brand_name,material,size,color,tp.status,tp.is_hot,tp.is_home,is_sale,sale_type,sale_value
			FROM    temp_product as tp
			JOIN    product_category as pc 
			ON      tp.pcat_code = pc.pcat_code
			JOIN    brand as b 
			ON      b.brand_alias = tp.brandcode
			WHERE   is_sale = 1
			LIMIT   $start,$num ");
		
		if($q->num_rows() > 0){
			foreach($q->result() as $row){
				$data[]=$row;
			}
			return $data;
		}
	}
	// Filter Category Nam
	function FcNam(){
		$q = $this->db->query("
			SELECT pc.pcat_code,pcat_name,menu_name
			FROM product_category AS pc 
			JOIN menu
			ON pc.menu_id = menu.menu_id
			WHERE pc.menu_id=1 ");	
		if($q->num_rows() > 0){
			foreach($q->result() as $row){
				$data[]=$row;
			}
			return $data;
		}
	}
	// Filter Category Nu
	function FcNu(){
		$q = $this->db->query("
			SELECT pc.pcat_code,pcat_name,menu_name
			FROM product_category AS pc 
			JOIN menu
			ON pc.menu_id = menu.menu_id
			WHERE pc.menu_id=2 ");	
		if($q->num_rows() > 0){
			foreach($q->result() as $row){
				$data[]=$row;
			}
			return $data;
		}
	}
	// Filter Brand
	function FilterBrand(){
		$q = $this->db->query("
			SELECT brand_alias,brand_name
			FROM brand  ");
		if($q->num_rows() > 0){
			foreach($q->result() as $row){
				$data[]=$row;
			}
			return $data;
		}
	}
	// Filter Material
	function FilterMaterial(){
		$q = $this->db->query("
			SELECT DISTINCT material 
			FROM temp_product
			WHERE material !='' ");
		if($q->num_rows() > 0){
			foreach($q->result() as $row){
				$data[]=$row;
			}
			return $data;
		}
	}
	// Create product
	function addProduct($data){
		$this->db->insert('temp_product',$data);
		return;
	}
	// Update Product
	function updateProduct($data,$id){
		$this->db->where('p_id',$id);
		$this->db->update('temp_product',$data);
		return;
	}
	// update sale
	function updateSale($data,$id){
		foreach($id as $item){
			echo $item;
			$this->db->where('p_id',$item);
			$this->db->update('temp_product',$data);
		}
		return;
	}
	// Get once product
	function getOnceProduct($id){
		$q= $this->db->query("
			SELECT  p_id,p_code,p_name,tp.pcat_code,pcat_name,brandcode,price,brand_name,material,size,color,
				    tp.status,tp.is_hot,tp.is_home,is_sale,sale_type,sale_value,date
			FROM    temp_product as tp
			JOIN    product_category as pc 
			ON      tp.pcat_code = pc.pcat_code
			JOIN    brand as b 
			ON      b.brand_alias = tp.brandcode
			WHERE   p_id=".$id);
		
		if($q->num_rows() == 1)
			 return $q->row();
	}
}


?>